<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="wCount.*" import="java.io.*" import="java.util.*"
	import="java.util.Map"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/Main.css" type="text/css">
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	%>

	<div id="div">
		<form action="show.jsp" method="post" class="filech" id="filech"
			name="filech">
			<fieldset class="filefield">
				<legend>请选择一个文件：</legend>
				<input type="input" name="file" placeholder="请在此输入文件路径"> <input
					type="hidden" value="" name="ch"> <input type="submit"
					value="上传" id="1" name="1" onclick="ch.value= '上传' "
					onchange="selectxt(this)">
			</fieldset>

			<%
				String text = "";
				String ch = request.getParameter("ch");
				if (ch.equals("上传")) {
					try {
						String getfile = request.getParameter("file");
						System.out.println(getfile);
						System.out.println(getfile.substring(getfile.length() - 3, getfile.length()));
						if (!getfile.substring(getfile.length() - 3, getfile.length()).equals("txt"))
							throw new Exception();
						File file = new File(getfile);
						FileDeal fd = new FileDeal();
						text = fd.FileToString(file);
						
			%>


			<fieldset class="textfield">
				<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
					name="textarea"><%=text%></textarea>
			</fieldset>
		</form>

		<textarea class="dealtext" id="result" readonly></textarea>
		<%
			} catch (Exception e) {
		%>


		<fieldset class="textfield">
			<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
				name="textarea"><%=text%></textarea>
		</fieldset>
		</form>

		<textarea class="dealtext" id="result" readonly></textarea>
		<%
			System.out.println(e);
					out.print("<script>alert('上传文件失败，请上传正确的txt文件！'); </script>");
				}
			}
			if (ch.equals("详细统计")) {
				String t = request.getParameter("textarea");
				WordDeal wd = new WordDeal(t);
				int charnum = wd.getCharCount();
				int wordcount = wd.getWordCount();
				int line = wd.getLineCount();
				Map<String, Integer> wc = wd.getWordFreq();//词频统计
				List list = wd.sortMap(wc);
				String[] wFreq = wd.ListToArray2(list);
		%>
		<fieldset class="textfield">
			<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
				name="textarea"><%=t%></textarea>
		</fieldset>
		</form>

		<textarea class="dealtext" id="result" readonly>字符数为：<%=charnum%>

单词数为：<%=wordcount%>

有效行数为：<%=line%>

词频数：
<%for (int i = 0; i < wFreq.length; i++) {%>
<%=wFreq[i]%>
<%}%></textarea>

	</div>
	<%
		}
			if (ch.equals("字符统计")) {
				String t = request.getParameter("textarea");
				WordDeal wd = new WordDeal(t);
				int charnum = wd.getCharCount();
				%>
				<fieldset class="textfield">
			<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
				name="textarea"><%=t%></textarea>
		</fieldset>
		</form>

		<textarea class="dealtext" id="result" readonly>字符数为：<%=charnum%></textarea>
		<%
			}

			if (ch.equals("单词统计")) {
				String t = request.getParameter("textarea");
				WordDeal wd = new WordDeal(t);
				int wordcount = wd.getWordCount();
				%>
				<fieldset class="textfield">
			<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
				name="textarea"><%=t%></textarea>
		</fieldset>
		</form>

		<textarea class="dealtext" id="result" readonly>单词数为：<%=wordcount%></textarea>
		<%
			}
			if (ch.equals("行数统计")) {
				String t = request.getParameter("textarea");
				WordDeal wd = new WordDeal(t);
				int line = wd.getLineCount();
				%>
				<fieldset class="textfield">
			<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
				name="textarea"><%=t%></textarea>
		</fieldset>
		</form>

		<textarea class="dealtext" id="result" readonly>有效行数为：<%=line%></textarea>
		<%
			}
			if (ch.equals("词频统计")) {
				String t = request.getParameter("textarea");
				WordDeal wd = new WordDeal(t);
				Map<String, Integer> wc = wd.getWordFreq();//词频统计
				List list = wd.sortMap(wc);
				String[] wFreq = wd.ListToArray2(list);
				%>
				<fieldset class="textfield">
			<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
				name="textarea"><%=t%></textarea>
		</fieldset>
		</form>

		<textarea class="dealtext" id="result" readonly>词频数：
<%for (int i = 0; i < wFreq.length; i++) {%>
<%=wFreq[i]%>
<%}%></textarea>

		<%
			}
			if (ch.equals("词组统计")) {

				String t = request.getParameter("textarea");
				if(request.getParameter("group")=="")
				{
					out.write("<script>alert('请输入词组长度！'); </script>");
					%>
					<fieldset class="textfield">
					<textarea  id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本" name="textarea"><%=t%></textarea>
				</fieldset>
				
			</form>
			
			<textarea class="dealtext" id="result" readonly ></textarea>
				<% }
				else{
				int num = Integer.parseInt(request.getParameter("group"));
				WordDeal wd = new WordDeal(t);
				Map<String, Integer> wg = wd.getWordGroupFreq(num);//词频统计
				List list = wd.sortMap(wg);
				String[] wgFreq = wd.ListToArray2(list);
				%>
				<fieldset class="textfield">
			<textarea id="textarea" placeholder="请点击上面按钮选择文件或直接在此输入文本"
				name="textarea"><%=t%></textarea>
		</fieldset>
		</form>

			<textarea class="dealtext" id="result" readonly>词组数：
<%for (int i = 0; i < wgFreq.length; i++) {%>
<%=wgFreq[i]%>
<%}%></textarea>
		<%}
			}
	%>

	<div class="but">
		<input type="submit" form="filech" value="详细统计" id="2" name="2"
			onclick="ch.value= '详细统计' " /> <input type="submit" form="filech"
			value="字符统计" id="3" name="3" onclick="ch.value= '字符统计' " /> <input
			type="submit" form="filech" value="行数统计" id="4" name="4"
			onclick="ch.value= '行数统计' " /> <input
			type="submit" form="filech" value="单词统计" 
			onclick="ch.value= '单词统计' " /><input type="submit" form="filech"
			value="词频统计" id="5" name="5" onclick="ch.value= '词频统计' " /> <input
			type="number" form="filech" placeholder="请输入词组数量" name="group" /> <input
			type="submit" form="filech" value="词组统计" id="6" onclick= "ch.value= '词组统计' " />
	</div>





	<script type="text/javascript">
		var txt = '';
		function selectxt(file) {
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {

				txt = evt.target.result;
			}
			reader.readAsDataURL(file.files[0]);
		}
	</script>

</body>
</html>